package edu.palermo.dondeestoy;

import java.util.Random;
import utils.Utils;
import android.app.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
//import positioncysand.wk.HTService;


public class WelcomeActivity extends Activity {

	 public static String Welcome = "Welcome:";
	 public static final String NombrePref = "DondeEstoyPref";
	 
	//private TelephonyManager mTelManager = (TelephonyManager)this.getSystemService(TELEPHONY_SERVICE);
	  
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);	
		final Utils utils = new Utils(this);
		
		setContentView(R.layout.welcome);    

	    Log.i(Welcome,"[onCreate] Dentro Content");
	    
	    Button button = (Button) findViewById(R.id.buttonIngresar);	    
	    
		////////////////////IMEI//////////////////////////////////////////
	    
	    TextView txIDUser =(TextView) findViewById(R.id.TextViewIDUser);
	    Log.i(Welcome,"[onCreate] IMEI:"+utils.getIMEI());
	    txIDUser.setText(utils.getIMEI());	    
	    
	    //////////////////////////////CODE//////////////////////////////
	    Log.i(Welcome,"[onCreate] VOY POR CODE ");
	    
		    Random rd=new Random();
			String strCodActUser=String.valueOf(rd.nextInt(1000000));				
			strCodActUser="CYS"+strCodActUser;
			
		    TextView txCodeUser = (TextView) findViewById(R.id.TextViewCode);
		    
		    txCodeUser.setText(strCodActUser);
		    
		    Log.i(Welcome,"[onCreate] CODE NULL - NEW CODE: "+strCodActUser);
		    utils.setCodeCYS(strCodActUser);
	    
	    
	    //TextView txViewSenderTime = (TextView) findViewById(R.id.textViewSenderTime);
	    
	    button.setOnClickListener(new OnClickListener()
	    {     
			public void onClick(View v) 
			{				
				 final EditText edTextTime = (EditText) findViewById(R.id.editTextTime);				
				
			    ////////////////////////////// TIMER SENDER //////////////////////////////
			    	if((edTextTime.getText().toString()!=null)&&(!edTextTime.getText().toString().equals(""))&&(!edTextTime.getText().toString().equals(" ")))			
					{
						utils.setTimer(edTextTime.getText().toString());
					    try
					    {
					        Log.i(Welcome,"[onCreate] Voy a empezar activity. TIME: "+edTextTime.getText().toString());
					    	startActivity(new Intent(getApplication(), MainActivity.class));
					    }catch(Exception Ex)
					    {
					    	Log.e(Welcome,"[onCreate] Exception Welcome start: "+Ex);	
					    }					    
					}else
					{
						Toast.makeText(getApplicationContext(),"ERROR - ENTER TIME CORRECTLY",Toast.LENGTH_SHORT).show();
					}	
			    			
			}
	    });
	 
	}	
	
////////////////////////////////////////////////////////////////////////////////////////////////
	public void onBackPressed() {
		Log.i("WelcomeActivity", "[onBackPressed] onBackPressed");
		
		Intent startMain = new Intent(Intent.ACTION_MAIN);
    	startMain.addCategory(Intent.CATEGORY_HOME);
    	startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    	startActivity(startMain);
	}
	
}